package cn.tedu.autoshow.website.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.util.PageInfoToPageDataConverter;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.website.mapper.PaperDetailMapper;
import cn.tedu.autoshow.website.mapper.PaperMapper;
import cn.tedu.autoshow.website.pojo.entity.Paper;
import cn.tedu.autoshow.website.pojo.entity.PaperDetail;
import cn.tedu.autoshow.website.pojo.param.PaperAddNewParam;
import cn.tedu.autoshow.website.pojo.param.PaperUpdateInfoParam;
import cn.tedu.autoshow.website.pojo.vo.PaperListItemVO;
import cn.tedu.autoshow.website.pojo.vo.PaperStandardVO;
import cn.tedu.autoshow.website.service.PaperService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PaperServiceImpl implements PaperService{

    @Autowired
    PaperMapper mapper;
    @Autowired
    PaperDetailMapper paperDetailMapper;


    //添加稿件
    public void addNew(PaperAddNewParam paperAddNewParam, cn.tedu.autoshow.commons.security.LoginPrincipal loginPrincipal) {
        log.info("【开始】:【新增—稿件文本】");
        log.info("传入参数, paperAddNewParam={}", paperAddNewParam);

        //判断稿件标题是否已存在,如果存在抛出异常
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", paperAddNewParam.getTitle());
        int countByName = mapper.selectCount(queryWrapper);
        log.info("根据稿件标题统计匹配的稿件数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "添加稿件失败，稿件标题已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //新增稿件文本并将新的稿件数据添加到数据库
        Paper paper = new Paper();
        BeanUtils.copyProperties(paperAddNewParam, paper);
        paper.setGmtCreate(LocalDateTime.now())
                .setUserId(loginPrincipal.getId())
                .setGmtModified(LocalDateTime.now());
        int rows = mapper.insert(paper);
        if (rows != 1) {
            String message = "添加稿件失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
        }
        log.info("将新的稿件数据插入到数据库，完成！");
    }

    @Override
    public void deleteById(Long id) {
        //判断稿件是否存在
        log.debug("开始处理【根据ID删除稿件】的业务，参数：{}", id);
        // 检查稿件是否存在，如果不存在，则抛出异常
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = mapper.selectCount(queryWrapper);
        log.debug("根据稿件ID统计匹配的稿件数量，结果：{}", countById);
        if (countById == 0) {
            String message = "删除稿件失败，稿件数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查稿件是否存在，如果存在，则抛出异常
        QueryWrapper<PaperDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", id);
        int countById1 = paperDetailMapper.selectCount(queryWrapper1);
        log.info("根据稿件详情ID统计匹配的稿件详情数量，结果：{}", countById1);
        if (countById1 > 0) {
            String message = "删除稿件失败，稿件详情存在,不允许删除！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
            //TODO 判断稿件详情表是否存在，存在不能删除

            //删除失败，抛出异常
            int rows = mapper.deleteById(id);
            if (rows != 1) {
                String message = "删除品牌失败，服务器忙，请稍后再试！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
            }
        }


        @Override
        public void updateInfoById(Long id, PaperUpdateInfoParam paperUpdateInfoParam) {
            //检查稿件是否存在
            log.debug("开始处理【根据ID修改稿件】的业务，参数：{}", id);
            // 检查稿件是否存在，如果不存在，则抛出异常
            QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            int countById = mapper.selectCount(queryWrapper);
            log.debug("根据稿件ID统计匹配的稿件数量，结果：{}", countById);
            if (countById == 0) {
                String message = "删除稿件失败，稿件数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }

            Paper brand = new Paper();
            BeanUtils.copyProperties(paperUpdateInfoParam, brand);
            brand.setId(id);
            int rows = mapper.updateById(brand);
            if (rows != 1) {
                String message = "修改稿件数据失败，服务器忙，请稍后再试！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
            }
            log.debug("将新的稿件数据更新入到数据库，完成！");
        }

    @Override
    public PaperStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询稿件】的业务，参数：{}", id);
        PaperStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询稿件失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<PaperListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<PaperListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询稿件列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<PaperListItemVO> list = mapper.list();
        PageInfo<PaperListItemVO> pageInfo = new PageInfo<>(list);
        PageData<PaperListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

}



